package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;

@Cmd(code = GameProtocol.S_CROSS_PARTY_SEND_LUCKY_REWARD, desc = "")
public class CrossPartySendLuckyRewardCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        BanquetProto.CrossPatrySendLuckRewardMsg req = BanquetProto.CrossPatrySendLuckRewardMsg.parseFrom(packet.getBytes());
        long userId = req.getUserId();
        int partyType = req.getPartyType();
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(partyType);
        String partyName = banquetInfo.getName();
        String nickName = req.getNickName();
        String luckyReward = banquetInfo.getLockReward();
        UserInfo userInfo = UserMgr.getUserInfo(userId);
        if (userInfo == null) {
            BanquetMgr.getLogger().error("send party lucky reward, can't find user {}", userId);
            return;
        }
        boolean isKuafu = req.getIsKuafu();
        if (isKuafu) {
            String serverName = "";
            long ownerServerId = req.getOwnerServerId();
            ServerInfo serverInfo = ServerListMgr.getServerInfo(ownerServerId);
            if (serverInfo != null) {
                serverName = serverInfo.getServerName();
            }
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.CROSS_PARTY_LUCK_USER_TITLE, userInfo.getLanguage());
            String banquetName = MultipleLanguageMgr.getContent(partyName, userInfo.getLanguage());
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.CROSS_PARTY_LUCK_USER_CONTENT, userInfo.getLanguage()),
                    nickName + "", serverName, banquetName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), luckyReward, mailContent, mailTitle);
        } else {
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.BANQUET_LUCK_USER_TITLE, userInfo.getLanguage());
            String banquetName = MultipleLanguageMgr.getContent(partyName, userInfo.getLanguage());
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.BANQUET_LUCK_USER_CONTENT, userInfo.getLanguage()),
                    nickName + "", banquetName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), luckyReward, mailContent, mailTitle);
        }
    }
}
